/**
 *  Copyright (C) 2014 3D Repo Ltd
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

function moxingid(part){
    $("#inptchange").val(part);
    $("#inptchange").trigger("input");

}

(function () {
    "use strict";

    angular.module("3drepo")
        .directive("tree", tree);

    function tree() {
        return {
            restrict: "EA",
            templateUrl: "tree.html",
            scope: {
                filterText: "=",
                height: "="
            },
            controller: TreeCtrl,
            controllerAs: "vm",
            bindToController: true
        };
    }

    TreeCtrl.$inject = ["$scope", "$timeout", "$element", "TreeService", "ViewerService", "EventService"];

    function TreeCtrl($scope, $timeout, $element, TreeService, ViewerService, EventService) {

        var vm = this,
            promise = null,
            i = 0,
            length = 0;

        vm.nodes = [];
        vm.showTree = true;
        vm.showFilterList = false;
        vm.currentFilterItemSelected = null;
        vm.viewerSelectedObject = null;
        vm.showProgress = true;
        vm.progressInfo = "Loading full tree structure";

        promise = TreeService.init();
        var idbox;
        var allidsbox;
        promise.then(function (data) {
            vm.allNodes = [];
            vm.allNodes.push(data.nodes);
            vm.nodes = vm.allNodes;
            vm.showTree = true;
            vm.showProgress = false;

            vm.idToPath = data.idToPath;
            initNodesToShow();
            setupInfiniteScroll();
            //所有节点
            idbox=vm.nodes;
            allidsbox=vm.nodes;
            console.log(idbox);
            var allisshux=JSON.stringify(vm.nodes);
            sessionStorage.setItem("allisshux",allisshux);
        });

        function initNodesToShow () {
            vm.nodesToShow = [vm.allNodes[0]];
            vm.nodesToShow[0].level = 0;
            vm.nodesToShow[0].expanded = false;
            vm.nodesToShow[0].hasChildren = true;
            vm.nodesToShow[0].selected = false;
            vm.nodesToShow[0].toggleState = "visible";
        }

        vm.expand = function (_id) {

            console.log("^^^^^^^^^^^^^^^^expandtreeButton");
            var i,
                numChildren,
                index = -1,
                length,
                endOfSplice = false;

            for (i = 0, length = vm.nodesToShow.length; i < length; i += 1) {
                if (vm.nodesToShow[i]._id === _id) {
                    index = i;
                    break;
                }
            }
            if (index !== -1) {
                if (vm.nodesToShow[index].hasChildren) {
                    if (vm.nodesToShow[index].expanded) {
                        while (!endOfSplice) {
                            if (angular.isDefined(vm.nodesToShow[index + 1]) && vm.nodesToShow[index + 1].path.indexOf(_id) !== -1) {
                                vm.nodesToShow.splice(index + 1, 1);
                            } else {
                                endOfSplice = true;
                            }
                        }
                    } else {
                        numChildren = vm.nodesToShow[index].children.length;
                        for (i = 0; i < numChildren; i += 1) {
                            vm.nodesToShow[index].children[i].expanded = false;
                            if (!vm.nodesToShow[index].children[i].hasOwnProperty("toggleState")) {
                                vm.nodesToShow[index].children[i].toggleState = vm.nodesToShow[index].toggleState;
                            }
                            vm.nodesToShow[index].children[i].level = vm.nodesToShow[index].level + 1;
                            vm.nodesToShow[index].children[i].hasChildren = vm.nodesToShow[index].children[i].children.length > 0;
                            vm.nodesToShow.splice(index + i + 1, 0, vm.nodesToShow[index].children[i]);
                        }
                    }
                    vm.nodesToShow[index].expanded = !vm.nodesToShow[index].expanded;
                }
            }
        };
        var cnoesid=[];
        var boif=null;

        function expandToSelection(path, level) {
            var hcsi=[];
            console.log(path);
            console.log(level);

            var idpar,
                idch,
                leftid,
                leftcid;
            var i,
                j,
                length,
                childrenLength,
                selectedId = path[path.length - 1],
                selectedIndex = 0,
                selectionFound = false;


            for (i = 0, length = vm.nodesToShow.length; i < length; i += 1) {

                if (vm.nodesToShow[i]._id === path[level]) {
                    vm.nodesToShow[i].expanded = true;
                    vm.nodesToShow[i].selected = false;
                    childrenLength = vm.nodesToShow[i].children.length;


                    if (level === (path.length - 2)) {
                        selectedIndex = i;
                    }
                    for (j = 0; j < childrenLength; j += 1) {
                        //vm.nodesToShow[i].children[j].selected=false;

                        vm.nodesToShow[i].children[j].selected = (vm.nodesToShow[i].children[j]._id === selectedId);
                        vm.nodesToShow[i].children[j].toggleState = "visible";
                        vm.nodesToShow[i].children[j].hasChildren = vm.nodesToShow[i].children[j].children.length > 0;


                        if(urlname=="plan"||urlname=="real"||urlname=="report"||urlname=="construct"||urlname=="check"||urlname=="patrol"||urlname=="build"||urlname=="division"||urlname=="branch"||urlname=="item"){
                            for(var qp=0;qp<leftextid.length;qp++){
                                if(vm.nodesToShow[i].children[j]._id==leftextid[qp]){
                                    hcsi.push({"par": i, "chil":j});
                                }
                            }
                        }else{
                            $(".ng-binding").removeClass("viewerSelected");
                        }

                        if (vm.nodesToShow[i].children[j].selected ) {


                            if(urlname=="plan"||urlname=="real"||urlname=="report"||urlname=="construct"||urlname=="check"||urlname=="patrol"||urlname=="build"||urlname=="division"||urlname=="branch"||urlname=="item"){
                                idpar=i;
                                idch=j;
                                for(var qpz=0;qpz<leftextid.length;qpz++){
                                    if(vm.nodesToShow[i].children[j]._id==leftextid[qpz]){

                                        console.log(i+"--"+j);
                                        leftextid.splice(qpz,1);
                                        console.log(leftextid);

                                        leftid=i;
                                        leftcid=j;
                                    }
                                }}
                            selectionFound = true;
                        }



                        if ((level === (path.length - 2)) && !selectionFound) {
                            selectedIndex += 1;
                        }
                        vm.nodesToShow[i].children[j].level = level + 1;
                        vm.nodesToShow.splice(i + j + 1, 0, vm.nodesToShow[i].children[j]);
                    }
                }
            }
            console.log(hcsi);


            if(urlname=="plan"||urlname=="real"||urlname=="report"||urlname=="construct"||urlname=="check"||urlname=="patrol"||urlname=="build"||urlname=="division"||urlname=="branch"||urlname=="item"){

                $.each(hcsi,function (ns,ls) {
                    vm.nodesToShow[ls.par].children[ls.chil].selected =true;
                });
                if(leftid!=undefined){
                    vm.nodesToShow[leftid].children[leftcid].selected =false;
                }
                if(cnoesid.length>0){

                    if(idpar!=undefined&&idch!=undefined){

                        for(var m=0;m<cnoesid.length;m++){
                            if(idpar==cnoesid[m].par&&idch==cnoesid[m].chil){
                                vm.nodesToShow[idpar].children[idch].selected=false;
                                cnoesid.splice(m,1);
                                console.log(cnoesid);
                                for(var yy=0;yy<leftextid.length;yy++){
                                    if(vm.nodesToShow[idpar].children[idch]._id==leftextid[yy]){
                                        leftextid.splice(yy,1);
                                        console.log(leftextid);
                                    }
                                }
                            }else {
                                if(m==cnoesid.length-1){
                                    if(leftid==undefined){

                                        cnoesid.push({"par": idpar, "chil": idch});
                                        leftextid.push(vm.nodesToShow[idpar].children[idch]._id);
                                    }
                                    console.log(leftextid);
                                    break;
                                }
                            }
                        }

                        console.log(cnoesid);
                        console.log(leftextid);
                        $.each(cnoesid,function (ns,ls) {
                            vm.nodesToShow[ls.par].children[ls.chil].selected =true;
                        });
                    }

                }else{
                    if(idpar!=undefined&&idch!=undefined) {

                        if(leftid==undefined){

                            cnoesid.push({"par": idpar, "chil": idch});
                            leftextid.push(vm.nodesToShow[idpar].children[idch]._id);
                        }
                    }
                }
            }









            if (level < (path.length - 2)) {
                expandToSelection(path, (level + 1));
            } else if (level === (path.length - 2)) {
                vm.topIndex = selectedIndex - 2;
            }

        }


        $(document).on("objectSelected", function (event, object) {
            $timeout(function () {
                console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",object);
                console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",event);
                if (angular.isUndefined(object)) {
                    vm.viewerSelectedObject = null;
                    vm.filterText = "";
                } else {
                    var idParts = null;
                    var path;

                    if (object["multipart"]) {
                        idParts = object.id.split("__");
                    } else {
                        idParts = object.getAttribute("id").split("__");
                    }
                    path = vm.idToPath[idParts[idParts.length - 1]].split("__");
                    console.log("@@@@@@@@@@@@@@@@@@selectednodepath",path)
                    initNodesToShow();

                    expandToSelection(path, 0);


                }
            });
        });

        var tempData = [];
        function eachChildren(data, reset = 1){
            if (reset){
                tempData = [];
            }
            var re = re;
            tempData.push(data._id);
            if(data.children.length){
                $.each(data.children, function(index, el){
                    eachChildren(el, 0);
                })
            }

            return tempData;
        }



        var allids= null;
        //点击构件勾选，选中与未选中操作建立链接发送到后台
        vm.toggleTreeNode = function (node) {
            console.log("****************toggleTreeNode",node);

            //当前选中所有_id
            var dataz = eachChildren(node);
            if (allids === null){
                //整个树形_id
                allids = eachChildren(idbox[0]);
            }

            var i = 0,
                j = 0,
                k = 0,
                nodesLength,
                path,
                parent = null,
                nodeToggleState = "visible",
                numInvisible = 0;

            vm.toggledNode = node;

            path = node.path.split("__");
            path.splice(path.length - 1, 1);
            // for(var p in vm.nodesToShow){
            // 	console.log("@@@@@@@@@@@@@@@@@@@@@@@nodesToShow",vm.nodesToShow[p]);
            // }

            for (i = 0, nodesLength = vm.nodesToShow.length; i < nodesLength; i += 1) {
                // Set node toggle state

                if (vm.nodesToShow[i]._id === node._id) {

                    console.log("^^^^^^^^^^^^^^^^^nodeSelectedisvisible:",vm.nodesToShow[i].toggleState === "visible");
                    vm.nodesToShow[i].toggleState = (vm.nodesToShow[i].toggleState === "visible") ? "invisible" : "visible";
                    console.log("%%%%%%%%%%%%%%%%%%%%%nodeToggleState",vm.nodesToShow[i].toggleState);
                    nodeToggleState = vm.nodesToShow[i].toggleState;

                    //建立长链接发送数据到后台
                    var diase=vm.nodesToShow[i].toggleState === "visible";
                    //如果是选中则合并两个数组_id
                    console.log(allids);
                    if (diase==true){
                        console.log("true");
                        allids = allids.concat(dataz);
                    }else {
                        console.log("false");
                        //未选中从全部_id中删除当前操作的_id
                        for(var k in dataz){
                            // var temp = allids.indexOf(data[i]);

                            allids.splice(allids.indexOf(dataz[k]), 1);
                        }
                    }
                    console.log(allids);
                    var data = {
                        _ids:allids,
                        type:diase
                    };

                    // sendSocket('showOrHide_component', data);通过树形菜单勾选，得到id新建编辑
                }
                // Set children to node toggle state
                else if (vm.nodesToShow[i].path.indexOf(node._id) !== -1) {
                    vm.nodesToShow[i].toggleState = nodeToggleState;
                }
                // Get node parent
                if (vm.nodesToShow[i]._id === path[path.length - 1]) {
                    parent = vm.nodesToShow[i];
                }
            }

            // Set the toggle state of the nodes above
            if (parent !== null) {
                for (i = (path.length - 1); i >= 0; i -= 1) {
                    for (j = 0, nodesLength = vm.nodesToShow.length; j < nodesLength; j += 1) {
                        if (vm.nodesToShow[j]._id === path[i]) {
                            vm.nodesToShow[j].toggleState = "visible";
                            numInvisible = 0;
                            for (k = 0; k < vm.nodesToShow[j].children.length; k += 1) {
                                if (vm.nodesToShow[j].children[k].toggleState === "invisible") {
                                    numInvisible += 1;
                                    vm.nodesToShow[j].toggleState = "parentOfInvisible";
                                } else if (vm.nodesToShow[j].children[k].toggleState === "parentOfInvisible") {
                                    vm.nodesToShow[j].toggleState = "parentOfInvisible";
                                }
                            }
                            if (numInvisible === vm.nodesToShow[j].children.length) {
                                vm.nodesToShow[j].toggleState = "invisible";
                            }
                        }
                    }
                }
            }
            toggleNode(node);
        };

        // $("#items").click(function(){
        //     alert("00000");
        //    alert(window.parent.document.getElementById("legend").html());
        // })
        // var ifmObj=obj.document.getElementById("planhtml");
        // ifmObj.click(function(){
        // 	alert("1111");
        // });

        var toggleNode = function (node) {
            console.log(node);

            var map = [];
            var pathArr = [];
            for (var obj in vm.idToPath) {
                if (vm.idToPath.hasOwnProperty(obj) && (vm.idToPath[obj].indexOf(node.path) !== -1)) {
                    pathArr = vm.idToPath[obj].split("__");
                    map.push(pathArr[pathArr.length - 1]);
                }
            }
            console.log("!!!!!!!!!!!!!!!!setVisibilityBySelectedNode");
            console.log(map);
            ViewerService.defaultViewer.setVisibilityByID(map, (node.toggleState === "visible"));
        };

        //选中文字控件改变颜色
        var urlname=getUrlParam("action");
        if(urlname=="plan"||urlname=="real"||urlname=="report"||urlname=="construct"||urlname=="check"||urlname=="patrol"||urlname=="build"||urlname=="division"||urlname=="branch"||urlname=="item"){

        }else{
            $("body").on("click",".ng-binding",function () {
                $(".ng-binding").removeClass("viewerSelected");
                $(this).addClass("viewerSelected");

            });
        }


        //根据链接地址截取sn参数
        function getUrlParam(name){
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r!=null) return unescape(r[2]); return null;
        }
        var names=getUrlParam('sn');
        if(names!=null){
            $("#leftButtons,#rightButtons,#rightPanel").css("display","none");
            $("#showButtonIcon").parent("button").css("display","none");
        }

        //关闭模型菜单
        if(getUrlParam('action')=="analysis"){
            $("#leftPanel").hide();
        }

        //模型管理显示隐藏菜单
        if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
            if(getUrlParam('action')=="index"){
                $("#leftPanel").css("margin-top","-1400px");
                $("#showButtonIcon").parent("button").css("display","block");
                $("#showButtonIcon").parent("button").css("margin-left","222px");

            }

        } else {
            if(getUrlParam('action')=="index"){
                $("#leftButtons,#rightButtons,#rightPanel").css("display","block");
                $("#showButtonIcon").parent("button").css("display","block");
                $("#showButtonIcon").parent("button").css("margin-left","222px");
            }
        }

        $("#showButtonIcon").click(function (){
            if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
                if(getUrlParam('action')=="index"){
                    $("#leftButtons,#rightButtons,#rightPanel").css("display","block");
                    $("#showButtonIcon").parent("button").css("display","block");


                    $("#showButtonIcon").parent("button").css("margin-left","222px");
                    // $("#leftPanel").css("margin-top","0");
                }
            }

        })







        //点击左侧构件文字调用id实现长链接
        var leftextid=[];
        var paretsid=[];
        vm.nodeSelected = function (node) {

            console.log(node);
            console.log("&&&&&&&&&&&&&&&&nodeSelected");
            var map = [];
            var pathArr = [];
            console.log(vm.idToPath);

            for (var obj in vm.idToPath) {

                if (vm.idToPath.hasOwnProperty(obj) && (vm.idToPath[obj].indexOf(node._id) !== -1)) {
                    //alert(vm.idToPath.hasOwnProperty(obj));
                    //alert(vm.idToPath[obj].indexOf(node._id));

                    pathArr = vm.idToPath[obj].split("__");
                    map.push(pathArr[pathArr.length - 1]);
                    var urlname=getUrlParam("action");

                    if(urlname=="plan"||urlname=="real"||urlname=="report"||urlname=="construct"||urlname=="check"||urlname=="patrol"||urlname=="build"||urlname=="division"||urlname=="branch"||urlname=="item"){

                        if(leftextid.length>0){
                            for(var q=0;q<leftextid.length;q++){
                                if(node._id==leftextid[q]){
                                    node.selected=false;
                                    leftextid.splice(q,1);
                                    console.log(leftextid);
                                    break;
                                }else {
                                    if(q==leftextid.length-1){
                                        node.selected=true;
                                        leftextid.push(node._id);
                                        console.log(leftextid);
                                        break;
                                    }

                                }
                            }

                        }else{
                            node.selected=true;
                            leftextid.push(node._id);
                        }

                    }
                    console.log(leftextid);


                }
            }

            //左侧菜单多选
            // if(urlname=="plan"||urlname=="real"||urlname=="report"||urlname=="construct"||urlname=="check"||urlname=="patrol"||urlname=="build"||urlname=="division"||urlname=="branch"||urlname=="item") {
            //
            //     if (node.children.length > 1) {
            //
            //         if (paretsid.length > 0) {
            //             for (var v = 0; v < paretsid.length; v++) {
            //                 if (node._id == paretsid[v]) {
            //
            //                     node.selected = false;
            //                     paretsid.splice(v, 1);
            //                     console.log(paretsid);
            //                     break;
            //                 } else {
            //                     if (v == paretsid.length - 1) {
            //
            //                         node.selected = true;
            //                         paretsid.push(node._id);
            //                         break;
            //                     }
            //                 }
            //             }
            //
            //         } else {
            //             node.selected = true;
            //             paretsid.push(node._id);
            //         }
            //
            //     }
            // }

            //通过文字改变模型颜色
            ViewerService.defaultViewer.selectPartsByID(map,false);
            EventService.send(EventService.EVENT.OBJECT_SELECTED, {id: node._id, name: node.name});




            var data = {
                _id:node._id
            };
            sendSocket('click_component', data);
            $("#rightPanel #items").css("opacity","0");

            $("#rightPanel #items div").click();


        };
        // $("#rightPanel #buttons .panelButtonGroup ").eq(0).children("label").text("问题");
        // $("#rightPanel #buttons .panelButtonGroup ").eq(1).children("label").text("刨面");
        // $("#rightPanel #buttons .panelButtonGroup ").eq(2).children("label").text("文档");



        $("body").on("click","#rightPanel #buttons .panelButtonGroup ",function () {
            $("#rightPanel #items").css("opacity","1");

            sendSocket('closeInfo_component', {});
        })
        // window.parent.document.getElementsByClassName(".addgoujian").hide();;

        var inputchange='<input id="inptchange" type="hidden">';
        $("body").before(inputchange);
        $("#inptchange").bind("input propertychange",function () {


            var nnbox=$("#inptchange").val();




            var data = {
                _id:nnbox
            };
            console.log(data);
            sendSocket('click_component', data);
            if(getUrlParam("action")=="Safety_index"){
                setTimeout(function(){
                    var data = {
                        _id:nnbox,
                        account:sessionStorage.getItem("account"),
                        norm:sessionStorage.getItem("norm"),
                        position:sessionStorage.getItem("position"),
                        project:sessionStorage.getItem("project")
                    };
                    console.log(data);

                    sendSocket('click_component', data);
                },500);
            }



            //点击安全点
            sendSocket('click_safetyPoint', data);
            $("#rightPanel #items").css("opacity","0");

            setTimeout(function () {
                var idspar=sessionStorage.getItem("idspart");

                var parids=[];
                parids=idspar.split(",");
                console.log(parids);
                var data = {
                    _ids:parids,
                    type:true
                };

                sendSocket('showOrHide_component', data);
            },1000)



            // $("#rightPanel #buttons .panelButtonGroup").click();

        });
        var animation;
        //webstork长链接
        var snbox =getUrlParam('sn');
        if(snbox){
            snbox=snbox.replace(/\s+/g,"+");
        }else{
            console.log("模型");

        }
        var rgbjih=null;
        var rgbwjh=null;
        var rgb02,rgb03,rgb04,rgb05;
        var server =server_config.bdiphost+ '/engine/'+snbox; // websocket服务器
        var ws = new WebSocket("ws://"+server);
        ws.onopen = function(){
            // 当连接成功时，会执行此处的代码
            // 发送消息到服务器
            // sendSocket('engineConnect', {});
        }
        var web_fd = '';
        ws.onmessage = function(response){
            // 此处执行收到消息后的业务逻辑
            var result = JSON.parse(response.data);
            var data = result.data;
            switch(result.type){
                case '':
                    // 记录WEB端fd连接标识
                    web_fd = data.web_fd;
                    break;
            }

            if(rgbjih===null){

                rgbjih=data.rgb[1];
                rgbwjh=data.rgb[0];
                rgb02=data.rgb[2];
                rgb03=data.rgb[3];
                rgb04=data.rgb[4];
                rgb05=data.rgb[5];

            }
            if(result.type=="create_projectTask"||result.type=="update_projectTask"){
                if(data.code==200){
                    tempData = [];
                    sessionStorage.setItem("addeditcolor",true);
                    //树形菜单勾选新建任务和编辑成功后，全部勾选
                    // $(".md-virtual-repeat-offsetter .repeated-item").eq(0).children(".layout-row").children(".treeButton").eq(1).click();

                }
            }
            if(result.type=="relationModel_constructionDaily"||result.type=="relationModel_constructionProgress"){
                sessionStorage.setItem("addeditcolor",true);
            }
            if(result.type=="inputReal_component"){
                //实际进度录入成功后，勾选全部树形菜单
                sessionStorage.setItem("addeditcolor",true);
                // $(".md-virtual-repeat-offsetter .repeated-item").eq(0).children(".layout-row").children(".treeButton").eq(1).click();
            }

            //模型动画
            if(result.type=="plannedTask_projectTask"){
                animation=result.data;

            }
            if(result.type=="drag_projectTask"){
                console.log(data.date);
                var timestamp = Date.parse(new Date(data.date));
                timestamp = timestamp / 1000;

                var anmation=[];
                $.each(animation.components,function (t,ls) {
                    //console.log(new Date(ls.taskdata.plan_begin_time*1000).toLocaleDateString());
                    if(ls.taskdata.plan_begin_time<=timestamp){
                        anmation.push(ls._id);
                    }

                })

                ViewerService.defaultViewer.setVisibilityByID(eachChildren(idbox[0]),false);
                ViewerService.defaultViewer.setVisibilityByID(anmation,true);
            }

            if(result.type=="create_planNode"){
                //节点创建成功，对未计划和计划的构件颜色进行改变0
                //全部id变成灰色
                if(data.code==200){

                    //alert(result.data.progress)chodikkkkdfoijf666;

                    yanse();
                    $(".md-virtual-repeat-offsetter .repeated-item").eq(0).children(".layout-row").children(".treeButton").eq(1).click();

                }else{
                }
            }
            if(result.type=="click_projectNode"){
                sessionStorage.setItem("datouzheng","true");
                yanse();
            }
            //编辑计划进度
            if(result.type=="update_planNode"||result.type=="update_planGradation"){
                if(data.code==200){

                    yanse();
                }else{
                }
            }

            if(result.type=="searchPlanByTime_component"){
                if(data.code==200){

                    yanse();
                }else{
                    alert(data.msg);
                }
            }
            if(result.type=="index_safetyPoint"){
                console.log(data);
                var ndx=JSON.stringify(data.info);
                sessionStorage.setItem("anqundiansz",ndx);

            }

            if(result.type=="component_danger"){
                console.log(data);
                var wxd=JSON.stringify(data.safetyPoint);
                sessionStorage.setItem("weixiandian",wxd);
                sendSocket('closeInfo_component', {});

            }

            //日报管理管理模型
            if(result.type=="showRelation_constructionDaily"){
                if(data.code==200){

                    yanse();
                }else{
                    alert(data.msg);
                }
            }

            //施工进度
            if(result.type=="showRelation_constructionProgress"){
                if(data.code==200){

                    yanse();
                }else{
                    alert(data.msg);
                }
            }

            //点击进度表展示模式
            if(result.type=="click_projectTask"||result.type=="show_planGradation"){

                if(data.code==200){

                    yanse();
                    tempData = [];


                }else{
                }
            }
            //日计划
            if(result.type=="inputPlan_component"){
                if(data.code==200){

                    yanse();
                }
            }
            //进度分析
            if(result.type=="planSearch_component"){
                if(data.code==200){

                    yanse();
                }
            }
            if(result.type=="showRelation_qualityCheck"){
                if(data.code==200){
                    yanse();
                }

            }
            //质量巡查
            if(result.type=="showRelation_qualityPatrol"){
                if(data.code==200){
                    yanse();
                }
            }

            //施工交底
            if(result.type=="showRelation_constructionBottom"){
                if(data.code==200){
                    yanse();
                }
            }

            //点击状态显示隐藏模型
            if(result.type=="show_projectTask"||result.type=="showSome_realProgress"||result.type=="showSome_planNode"||result.type=="showSome_planGradation"||result.type=="showSome_realProgress"){
                if(data.code==200){
                    console.log(result.data);
                    if(result.type=="show_projectTask"||result.type=="showSome_planNode"||result.type=="showSome_planGradation"){
                        if(result.data.components==0){
                            ViewerService.defaultViewer.setVisibilityByID(eachChildren(idbox[0]),false);
                        }else if(result.data.components==1){
                            ViewerService.defaultViewer.setVisibilityByID(eachChildren(idbox[0]),true);
                        }else{
                            ViewerService.defaultViewer.setVisibilityByID(eachChildren(idbox[0]),false);
                            ViewerService.defaultViewer.setVisibilityByID(result.data.components,true);
                        }

                    }
                    if(result.type=="showSome_realProgress"||result.type=="showSome_realProgress"){

                        var nx=result.data.tags.length;
                        var jobxz00 = sessionStorage.getItem("ztboxys00");
                        var ztboxys00=[];
                        ztboxys00=jobxz00.split(",");

                        var jobxz01= sessionStorage.getItem("ztboxys01");
                        var ztboxys01=[];
                        ztboxys01=jobxz01.split(",");

                        var jobxz02= sessionStorage.getItem("ztboxys02");
                        var ztboxys02=[];
                        ztboxys02=jobxz02.split(",");

                        var jobxz03 = sessionStorage.getItem("ztboxys03");
                        var ztboxys03=[];
                        ztboxys03=jobxz03.split(",");

                        var jobxz04= sessionStorage.getItem("ztboxys04");
                        var ztboxys04=[];
                        ztboxys04=jobxz04.split(",");

                        var jobxz05 = sessionStorage.getItem("ztboxys05");
                        var ztboxys05=[];
                        ztboxys05=jobxz05.split(",");

                        var yansbox=[];
                        for(var nn=0;nn<nx;nn++){
                            if(result.data.tags[nn]==0){
                                yansbox =yansbox.concat(ztboxys00);
                            }
                            if(result.data.tags[nn]==1){
                                yansbox =yansbox.concat(ztboxys01);
                            }
                            if(result.data.tags[nn]==2){
                                yansbox =yansbox.concat(ztboxys02);
                            }
                            if(result.data.tags[nn]==3){
                                yansbox =yansbox.concat(ztboxys03);
                            }
                            if(result.data.tags[nn]==4){
                                yansbox =yansbox.concat(ztboxys04);
                            }
                            if(result.data.tags[nn]==5){
                                yansbox =yansbox.concat(ztboxys05);
                            }
                        }

                        ViewerService.defaultViewer.setVisibilityByID(eachChildren(idbox[0]),false);
                        ViewerService.defaultViewer.setVisibilityByID(yansbox,true);




                    }


                }
            }
            //模型加载完毕

            function yanse(){
                console.log(result);
                console.log(result.data);
                var qxbox;
                var xzbox
                if(result.type=="planSearch_component"){
                    qxbox=data.rgb[1];
                    xzbox=data.rgb[0];
                }else if(result.type=="click_projectNode"||result.type=="click_projectTask"||result.type=="showRelation_qualityCheck"||result.type=="showRelation_qualityPatrol"||result.type=="showRelation_constructionBottom"){

                    qxbox=rgbjih;
                    xzbox=rgbwjh;
                }else{
                    qxbox=result.data.progress;
                    xzbox=result.data.unprogress;
                }
                qxbox=qxbox.replace("[","");
                qxbox=qxbox.replace("]","");

                xzbox=xzbox.replace("[","");
                xzbox=xzbox.replace("]","");
                sessionStorage.setItem("qxbox",qxbox);
                sessionStorage.setItem("xzbox",xzbox);
                sessionStorage.setItem("moxingys","true");
                // ViewerService.defaultViewer.ssetVisibilityByID(eachChildren(idbox[0]), true);
                //选中id变成亮色
                if(result.type=="click_projectTask"||result.type=="showRelation_constructionBottom"||result.type=="showRelation_qualityPatrol"||result.type=="showRelation_qualityCheck"||result.type=="planSearch_component"||result.type=="showRelation_constructionProgress"||result.type=="show_planNode"||result.type=="show_planGradation"||result.type=="searchPlanByTime_component"||result.type=="showRelation_constructionDaily"){
                    allids=result.data.component_ids;
                    console.log(allids);
                }
                if(result.type=="click_projectNode"){
                    allids=result.data.components;
                    console.log(allids);
                }


                sessionStorage.setItem("localjh",allids);
                setTimeout(function () {
                    ViewerService.defaultViewer.qxselectPartsByID(eachChildren(idbox[0]),false);
                    setTimeout(function () {
                        ViewerService.defaultViewer.xzselectPartsByID(allids,false);
                    }, 20);

                }, 20);
                ViewerService.defaultViewer.setVisibilityByID(eachChildren(idbox[0]),false);
                console.log(allids);
                ViewerService.defaultViewer.setVisibilityByID(allids,true);


                // for(var bb=0;bb<allidboxz;bb++){
                //     for(var cc=0;cc<xuanzhongbox;cc++){
                //         if(eachChildren(idbox[0])[bb]!=allids[cc]){
                //
                //             undefinez.push(eachChildren(idbox[0])[bb]);
                //
                //         }
                //     }
                // }
                // console.log(undefinez);






            }
            //add by zhoufangxu at 2018.9.14  查找父类方法

            function doParentMethod(method,args){
                var parent=window.top;
                var params={'method':method,'arguments':JSON.stringify(args)};
                var finalMethod=undefined;
                try{
                    parent.postMessage(params,'*');
                }catch(e){
                    finalMethod=undefined;
                }

                return finalMethod;
            }
            function checkIdsIsRight(allIds,ids){
                var finalExist=true;
                for(var i=0,leni=ids.length;i<leni;i++){
                    var isExist=false;
                    for(var j=0,lenj=allIds.length;j<lenj;j++){
                        if(ids[i]==allIds[j]){
                            isExist=true;
                        }
                    }
                    if(!isExist){
                        finalExist=false;
                        break;
                    }
                }
                return finalExist;
            }
            console.log(result.type);
            console.log(data);
            if(sessionStorage.getItem("onloadloa")=="true"){
                if(data.code==200){
                    var allids=eachChildren(idbox[0]);
                    switch(result.type)
                    {
                        case 'showRelation_unitEngineering':
                        case 'showRelation_projectDivision':
                        case 'showRelation_subProject':
                            if(!data.component_ids||!data.component_ids[0]||data.component_ids.length==0){
                                doParentMethod('layer.alert',['初次全部加载']);
                                ViewerService.defaultViewer.setVisibilityByID(eachChildren(idbox[0]),false);
                                ViewerService.defaultViewer.setVisibilityByID(eachChildren(idbox[0]),true);
                            }else{
                                if(checkIdsIsRight(allids,data.component_ids)){
                                    ViewerService.defaultViewer.setVisibilityByID(eachChildren(idbox[0]),false);
                                    ViewerService.defaultViewer.setVisibilityByID(data.component_ids,true);
                                }else{
                                    doParentMethod('layer.alert',['模型已被更新']);
                                    ViewerService.defaultViewer.setVisibilityByID(eachChildren(idbox[0]),false);
                                    ViewerService.defaultViewer.setVisibilityByID(eachChildren(idbox[0]),true);
                                }
                            }
                            break;
                    }
                }
            }

            //模型加载完毕实际进度
            if(result.type=="index_realProgress"||result.type=="reload_realProgress"||result.type=="inputReal_component") {
                sessionStorage.setItem("localwjh",eachChildren(idbox[0]));

                if (getUrlParam('action') == "real" || getUrlParam('action') == "compare"||getUrlParam('action') == "analysis") {
                    var ztboxys00 = [];
                    var ztboxys01 = [];
                    var ztboxys02 = [];
                    var ztboxys03 = [];
                    var ztboxys04 = [];
                    var ztboxys05 = [];

                    var moxleng = data.components.length;

                    console.log(data);


                    rgbwjh=rgbwjh.replace("[","");
                    rgbwjh=rgbwjh.replace("]","");

                    rgbjih=rgbjih.replace("[","");
                    rgbjih=rgbjih.replace("]","");

                    rgb02=rgb02.replace("[","");
                    rgb02=rgb02.replace("]","");

                    rgb03=rgb03.replace("[","");
                    rgb03=rgb03.replace("]","");

                    rgb04=rgb04.replace("[","");
                    rgb04=rgb04.replace("]","");

                    rgb05=rgb05.replace("[","");
                    rgb05=rgb05.replace("]","");

                    sessionStorage.setItem("ysz00", rgbwjh);
                    sessionStorage.setItem("ysz01", rgbjih);
                    sessionStorage.setItem("ysz02", rgb02);
                    sessionStorage.setItem("ysz03", rgb03);
                    sessionStorage.setItem("ysz04", rgb04);
                    sessionStorage.setItem("ysz05", rgb05);


                    for (var i = 0, l = moxleng; i < l; i++) {

                        if (data.components[i]['status'] == "0") {
                            ztboxys00.push(data.components[i]['_id']);
                        }
                        if (data.components[i]['status'] == "1") {
                            ztboxys01.push(data.components[i]['_id']);
                        }
                        if (data.components[i]['status'] == "2") {
                            ztboxys02.push(data.components[i]['_id']);
                        }
                        if (data.components[i]['status'] == "3") {
                            ztboxys03.push(data.components[i]['_id']);
                        }
                        if (data.components[i]['status'] == "4") {
                            ztboxys04.push(data.components[i]['_id']);
                        }
                        if (data.components[i]['status'] == "5") {
                            ztboxys05.push(data.components[i]['_id']);
                        }
                        //ztboxys.push(data.components[i]['_id']);

                        var ztysid = data.components[i]['_id'];
                        // var ztysys=data.components[i]['real_color'];0
                        //     ztysys=ztysys.replace("[","");
                        //      ztysys=ztysys.replace("]","");
                        // sessionStorage.setItem("ztysysbox",ztysys);
                        //ViewerService.defaultViewer.ztysselectPartsByID(ztysid,false);

                    }
                    sessionStorage.setItem("ztboxys00", ztboxys00);
                    sessionStorage.setItem("ztboxys01", ztboxys01);
                    sessionStorage.setItem("ztboxys02", ztboxys02);
                    sessionStorage.setItem("ztboxys03", ztboxys03);
                    sessionStorage.setItem("ztboxys04", ztboxys04);
                    sessionStorage.setItem("ztboxys05", ztboxys05);
                    //ViewerService.defaultViewer.ssetVisibilityByID(eachChildren(idbox[0]), true);
                    // ViewerService.defaultViewer.qxselectPartsByID(eachChildren(idbox[0]),false);
                    ViewerService.defaultViewer.ztysselectPartsByID(ztboxys00, false, 0);
                    ViewerService.defaultViewer.ztysselectPartsByID(ztboxys01, false, 1);
                    ViewerService.defaultViewer.ztysselectPartsByID(ztboxys02, false, 2);
                    ViewerService.defaultViewer.ztysselectPartsByID(ztboxys03, false, 3);
                    ViewerService.defaultViewer.ztysselectPartsByID(ztboxys04, false, 4);
                    ViewerService.defaultViewer.ztysselectPartsByID(ztboxys05, false, 5);
                    // ViewerService.defaultViewer.setVisibilityByID(eachChildren(idbox[0]),false);
                    ViewerService.defaultViewer.setVisibilityByID(ztboxys00, false);
                    ViewerService.defaultViewer.setVisibilityByID(ztboxys01, false);
                    ViewerService.defaultViewer.setVisibilityByID(ztboxys02, true);
                    ViewerService.defaultViewer.setVisibilityByID(ztboxys03, true);
                    ViewerService.defaultViewer.setVisibilityByID(ztboxys04, true);
                    ViewerService.defaultViewer.setVisibilityByID(ztboxys05, true);


                }
            }




        }
        ws.onclose = function (e) {
            //alert('连接关闭');
        };
        ws.onerror = function (e) {
            // alert(e);
        };

        function sendSocket(type, data){


            data.sn =snbox;
            ws.send(JSON.stringify({
                type: type,
                data:data
            }));
        }

        var intimes=self.setInterval(function () {
            if(sessionStorage.getItem("onloadloa")=="true"){
                clearInterval(intimes);

                $("panel-content md-content").css("overflow-x",null);
                $("panel-content md-content").css("overflow",null);

                setTimeout(function(){
                    sendSocket('index_realProgress', {});
                    //获取安全点
                    if(getUrlParam('action')=="Safety_index"){
                        sendSocket('index_safetyPoint', {});
                    }

                    $("#leftPanel #items .md-toolbar-tools .md-flex").text("构件列表");
                    $("#leftPanel #buttons .ng-binding").text("构件");
                    $("#leftPanel #items .md-toolbar-tools .md-flex").css("margin-top","9px");
                    $("#rightPanel #items .md-toolbar-tools .md-flex ").text("问题");
                    $("#rightPanel #buttons .panelButtonGroup ").eq(0).children("label").text("问题");
                    $("#rightPanel #buttons .panelButtonGroup ").eq(1).children("label").text("刨面");
                    $("#rightPanel #buttons .panelButtonGroup ").eq(2).children("label").text("文档");
                    if(getUrlParam('action')=="plan"){
                        //修改左侧树行构件高度

                        $(".virtualRepeatInfiniteScroll .vertical-container").css("height","320px");
                        $("#items").css("height","380px");
                        $("#items").css("overflow","hidden");



                    }

                },1000);
            }
        },2000);



        // setTimeout(function(){
        //    ViewerService.defaultViewer.setVisibilityByID(eachChildren(idbox[0]),false);
        //    setTimeout(function(){
        //        ViewerService.defaultViewer.setVisibilityByID(eachChildren(idbox[0]),true);
        // 	},2000);
        //
        // },20000);



        function setupInfiniteScroll() {

            // Infinite items
            vm.infiniteItemsTree = {
                numLoaded_: 0,
                toLoad_: 0,

                getItemAtIndex: function (index) {
                    if (index > this.numLoaded_) {
                        this.fetchMoreItems_(index);
                        return null;
                    }

                    if (index < vm.nodesToShow.length) {
                        return vm.nodesToShow[index];
                    } else {
                        return null;
                    }
                },

                getLength: function () {
                    return this.numLoaded_ + 5;
                },

                fetchMoreItems_: function (index) {
                    if (this.toLoad_ < index) {
                        this.toLoad_ += 500;
                        $timeout(angular.noop, 300).then(angular.bind(this, function () {
                            this.numLoaded_ = this.toLoad_;
                        }));
                    }
                }
            };

            // for(var p in vm.infiniteItemsTree){
            // 	if(p.name="RDS RF01 (IFC Space)"){
            // 		console.log("&&&&&&&&&&&&&&&&&&&&isItemExpend",p.expanded);
            // 		console.log("%%%%%%%%%%%%%%%%%%%%%vm.infiniteItemsTree",vm.infiniteItemsTree);

            // 	}
            // }


        }

        $scope.$watch("vm.filterText", function (newValue) {

            if (angular.isDefined(newValue)) {
                if (newValue === "") {
                    vm.showTree = true;
                    vm.showFilterList = false;
                    vm.nodes = vm.allNodes;
                } else {
                    vm.showTree = false;
                    vm.showFilterList = false;
                    vm.showProgress = true;
                    vm.progressInfo = "Filtering tree for objects";

                    promise = TreeService.search(newValue);
                    promise.then(function (json) {
                        vm.showFilterList = true;
                        vm.showProgress = false;
                        vm.nodes = json.data;
                        for (i = 0, length = vm.nodes.length; i < length; i += 1) {
                            vm.nodes[i].index = i;
                            vm.nodes[i].toggleState = "visible";
                            vm.nodes[i].class = "unselectedFilterItem";
                        }
                        setupInfiniteItemsFilter();
                    });
                }
            }
        });




        vm.filterItemSelected = function (item) {

            console.log(item);

            if (vm.currentFilterItemSelected === null) {
                vm.nodes[item.index].class = "selectedFilterItem";
                vm.currentFilterItemSelected = item;
            } else if (item.index === vm.currentFilterItemSelected.index) {
                vm.nodes[item.index].class = "unselectedFilterItem";
                vm.currentFilterItemSelected = null;
            } else {
                vm.nodes[vm.currentFilterItemSelected.index].class = "unselectedFilterItem";
                vm.nodes[item.index].class = "selectedFilterItem";
                vm.currentFilterItemSelected = item;
            }
            TreeService.selectNode(vm.nodes[item.index]._id);

        };

        vm.toggleFilterNode = function (item) {

            item.toggleState = (item.toggleState === "visible") ? "invisible" : "visible";
            item.path = item._id;
            toggleNode(item);
        };

        function setupInfiniteItemsFilter() {

            vm.infiniteItemsFilter = {
                numLoaded_: 0,
                toLoad_: 0,
                getItemAtIndex: function (index) {
                    if (index > this.numLoaded_) {
                        this.fetchMoreItems_(index);
                        return null;
                    }

                    if (index < vm.nodes.length) {
                        return vm.nodes[index];
                    } else {
                        return null;
                    }
                },
                getLength: function () {
                    return this.numLoaded_ + 5;
                },
                fetchMoreItems_: function (index) {
                    if (this.toLoad_ < index) {
                        this.toLoad_ += 20;
                        $timeout(angular.noop, 300).then(angular.bind(this, function () {
                            this.numLoaded_ = this.toLoad_;
                        }));
                    }
                }
            };
        }

    }
}());